package com.kaikeba.dao.impl;

import com.kaikeba.dao.AdminDao;
import com.kaikeba.util.DruidUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class AdminDaoImpl implements AdminDao {
    /**
     * 根据登录用户名更新ip和登录时间
     *
     * @param username 用户名
     * @param ip       ip
     * @param date     登录时间
     */
    @Override
    public void updateLoginTime(String username, String ip, Date date) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement statement = null;
        try {
             statement = conn.prepareStatement("update eadmin set loginip=?,logintime=? where username=?");
             statement.setString(1,ip);
             statement.setDate(2,new java.sql.Date(date.getTime()));
             statement.setString(3,username);
             statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn,statement,null);
        }
    }

    /**
     * 根据用户名和密码进行登录
     *
     * @param username     用户名
     * @param password 密码
     * @return 返回结果 true表示成功
     */
    @Override
    public boolean login(String username, String password) {

        Connection conn = DruidUtil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = conn.prepareStatement("select id from eadmin where username=? and password=?");
            statement.setString(1,username);
            statement.setString(2,password);
            resultSet = statement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidUtil.close(conn,statement,resultSet);
        }
        return false;
    }
}
